#########################
### Appendix material ###
#########################


combo <- combo %>%
  mutate(Horizon = ifelse(Background == "LongHistory" & Inaction == "Continue", "LongHorizon", 
                          ifelse(Background == "RecentShift" & Inaction == "TickingClock", "ShortHorizon", 
                                 "BaseHorizon")),
         Effect = ifelse(Background == "LongHistory" & Rationale == "LongRun", "LongEffect", 
                         ifelse(Background == "RecentShift" & Rationale == "Immediate", "ShortEffect",
                                "BaseEffect"))
  )

combo$Horizon = factor(combo$Horizon)
combo$Horizon = relevel(combo$Horizon, ref = "BaseHorizon")

combo$Effect = factor(combo$Effect)
combo$Effect = relevel(combo$Effect, ref = "BaseEffect")


#H7 and H8 - time horizons
model <- DV_support ~ Horizon * Effect + Author + EffectOrd + country1 + Prior3 + badbehavior1*country1

mms <- cj(combo, model, 
          id = ~1, alpha=0.05, estimate = "mm", by = ~ Policy)

diff_mms <- cj(combo, model,
               id = ~1, alpha=0.05, estimate = "mm_diff", by = ~ Policy)


plot(rbind(mms, diff_mms)) + ggplot2::facet_wrap(~BY, ncol = 4L) + scale_color_jco() +
  labs(x="Support considering time frames") + theme(legend.position="none")


ggsave("Output/Rplot_H7_H8_mms.pdf",
       width = 10, height = 6, dpi = 300, units = "in", device='pdf')



# Separate by experiment
combo$experiment_no <- factor(combo$experiment_no)
#H1 & H4 & H5 & H6 - by experiment

model_issue_full <- DV_support ~ Policy + Background + Inaction + Author + badbehavior1 + Rationale + EffectOrd + Prior3 + country1 

amces_issue_full <- cj(combo, model_issue_full, 
                       id = ~1, alpha=0.05, estimate = "amce", by = ~ experiment_no)

plot(rbind(amces_issue_full)) + ggplot2::facet_wrap(~BY, ncol = 3L) + labs(x="Support by experiment")+ scale_color_jco() + theme(legend.position="none")

ggsave("Output/Rplot_byexp.pdf",
       width = 10, height = 6, dpi = 300, units = "in", device='pdf')



mms_issue_full <- cj(combo, model_issue_full, 
                     id = ~1, alpha=0.05, estimate = "mm", by = ~ experiment_no)

plot(rbind(mms_issue_full)) + ggplot2::facet_wrap(~BY, ncol = 3L) + labs(x="Support by experiment")+ scale_color_jco() + theme(legend.position="none")

ggsave("Output/Rplot_byexp_mm.pdf",
       width = 10, height = 6, dpi = 300, units = "in", device='pdf')


model_issue_pol_full <- DV_support ~ Background + Prior3 + badbehavior1 + country1 + Inaction + Author + Rationale + EffectOrd



amces_issue_pol_full <- cj(combo[combo$experiment_no == 1,], model_issue_pol_full, 
                           id = ~1, alpha=0.05, estimate = "amce", by = ~ Policy)

diff_amces_issue_pol_full <- cj(combo[combo$experiment_no == 1,], model_issue_pol_full, 
                                id = ~1, alpha=0.05, estimate = "amce_diff", by = ~ Policy)

plot(rbind(amces_issue_pol_full, diff_amces_issue_pol_full)) + ggplot2::facet_wrap(~BY, ncol = 3L) + labs(x="Support by experiment")+ scale_color_jco() + theme(legend.position="none")

ggsave("Output/Rplot_bypol_exp1_amces.pdf",
       width = 10, height = 6, dpi = 300, units = "in", device='pdf')


amces_issue_pol_full <- cj(combo[combo$experiment_no == 2,], model_issue_pol_full, 
                           id = ~1, alpha=0.05, estimate = "amce", by = ~ Policy)

diff_amces_issue_pol_full <- cj(combo[combo$experiment_no == 2,], model_issue_pol_full, 
                                id = ~1, alpha=0.05, estimate = "amce_diff", by = ~ Policy)

plot(rbind(amces_issue_pol_full, diff_amces_issue_pol_full)) + ggplot2::facet_wrap(~BY, ncol = 3L) + labs(x="Support by experiment")+ scale_color_jco() + theme(legend.position="none")

ggsave("Output/Rplot_bypol_exp2_amces.pdf",
       width = 10, height = 6, dpi = 300, units = "in", device='pdf')


amces_issue_pol_full <- cj(combo[combo$experiment_no == 3,], model_issue_pol_full, 
                           id = ~1, alpha=0.05, estimate = "amce", by = ~ Policy)

diff_amces_issue_pol_full <- cj(combo[combo$experiment_no == 3,], model_issue_pol_full, 
                                id = ~1, alpha=0.05, estimate = "amce_diff", by = ~ Policy)

plot(rbind(amces_issue_pol_full, diff_amces_issue_pol_full)) + ggplot2::facet_wrap(~BY, ncol = 3L) + labs(x="Support by experiment")+ scale_color_jco() + theme(legend.position="none")

ggsave("Output/Rplot_bypol_exp3_amces.pdf",
       width = 10, height = 6, dpi = 300, units = "in", device='pdf')



mms_issue_pol_full <- cj(combo[combo$experiment_no == 1,], model_issue_pol_full, 
                         id = ~1, alpha=0.05, estimate = "mm", by = ~ Policy)

diff_mms_issue_pol_full <- cj(combo[combo$experiment_no == 1,], model_issue_pol_full, 
                              id = ~1, alpha=0.05, estimate = "mm_diff", by = ~ Policy)

plot(rbind(mms_issue_pol_full, diff_mms_issue_pol_full)) + ggplot2::facet_wrap(~BY, ncol = 3L) + labs(x="Support by experiment")+ scale_color_jco() + theme(legend.position="none")

ggsave("Output/Rplot_bypol_exp1_mms.pdf",
       width = 10, height = 6, dpi = 300, units = "in", device='pdf')


mms_issue_pol_full <- cj(combo[combo$experiment_no == 2,], model_issue_pol_full, 
                         id = ~1, alpha=0.05, estimate = "mm", by = ~ Policy)

diff_mms_issue_pol_full <- cj(combo[combo$experiment_no == 2,], model_issue_pol_full, 
                              id = ~1, alpha=0.05, estimate = "mm_diff", by = ~ Policy)

plot(rbind(mms_issue_pol_full, diff_mms_issue_pol_full)) + ggplot2::facet_wrap(~BY, ncol = 3L) + labs(x="Support by experiment")+ scale_color_jco() + theme(legend.position="none")

ggsave("Output/Rplot_bypol_exp2_mms.pdf",
       width = 10, height = 6, dpi = 300, units = "in", device='pdf')


mms_issue_pol_full <- cj(combo[combo$experiment_no == 3,], model_issue_pol_full, 
                         id = ~1, alpha=0.05, estimate = "mm", by = ~ Policy)

diff_mms_issue_pol_full <- cj(combo[combo$experiment_no == 3,], model_issue_pol_full, 
                              id = ~1, alpha=0.05, estimate = "mm_diff", by = ~ Policy)

plot(rbind(mms_issue_pol_full, diff_mms_issue_pol_full)) + ggplot2::facet_wrap(~BY, ncol = 3L) + labs(x="Support by experiment")+ scale_color_jco() + theme(legend.position="none")

ggsave("Output/Rplot_bypol_exp3_mms.pdf",
       width = 10, height = 6, dpi = 300, units = "in", device='pdf')



# By party
model_test <- DV_support ~ Policy + Background + Prior3 + badbehavior1 * country1 + Inaction + Author + Rationale + EffectOrd


amces_test <- cj(combo, model_test, 
                 id = ~1, alpha=0.05, estimate = "amce", by = ~ party3)


plot(amces_test) + ggplot2::facet_wrap(~BY, ncol = 3L) + scale_color_jco() +
  labs(x="Support by Party")+ theme(legend.position="none")

ggsave("Output/Rplot_byparty.pdf",
       width = 10, height = 6, dpi = 300, units = "in", device='pdf')


model_test <- DV_support ~ Policy + Background + Prior3 + badbehavior1 * country1 + Inaction + Author + Rationale + EffectOrd


mm_test <- cj(combo, model_test, 
              id = ~1, alpha=0.05, estimate = "mm", by = ~ party3)

mm_diff_test <- cj(combo, model_test, 
                   id = ~1, alpha=0.05, estimate = "mm_diff", by = ~ party3)


plot(rbind(mm_test, mm_diff_test)) + ggplot2::facet_wrap(~BY, ncol = 3L) + scale_color_jco() + 
  labs(x="Support by Party")+ theme(legend.position="none")

ggsave("Output/Rplot_mm_byparty.pdf",
       width = 10, height = 10, dpi = 300, units = "in", device='pdf')


# Support by country
model <- DV_support ~ Policy + Background + Prior3 + badbehavior1 + Inaction + Author + Rationale + EffectOrd


amces <- cj(combo, model, 
            id = ~1, alpha=0.05, estimate = "amce", by = ~ country1)

plot(rbind(amces)) + ggplot2::facet_wrap(~BY, ncol = 5L) + scale_color_jco() +
  labs(x="Support by Country") + theme(legend.position="none")

ggsave("Output/Rplot_bycountry.pdf",
       width = 10, height = 12, dpi = 300, units = "in", device='pdf')


# By bad behavior
model <- DV_support ~ Policy + Background + Prior3 + country1 + Inaction + Author + Rationale + EffectOrd


mms <- cj(combo, model, 
          id = ~1, alpha=0.05, estimate = "mm", by = ~ badbehavior1)
diff_mms <- cj(combo, model,
               id = ~1, alpha=0.05, estimate = "mm_diff", by = ~ badbehavior1)

plot(rbind(mms, diff_mms)) + ggplot2::facet_wrap(~BY, ncol = 4L) + scale_color_jco() +
  labs(x="Support by Bad behavior/issue")+ theme(legend.position="none")

ggsave("Output/plot_byissue_comp.pdf",
       width = 9, height = 8, dpi = 300, units = "in", device='pdf')


# By prior view on country 1
model <- DV_support ~ Policy + Background + badbehavior1 * country1 + Inaction + Author + Rationale + EffectOrd


mms <- cj(combo, model, 
          id = ~1, alpha=0.05, estimate = "mm", by = ~ Prior)

diff_mms <- cj(combo, model,
               id = ~1, alpha=0.05, estimate = "mm_diff", by = ~ Prior)

plot(rbind(mms, diff_mms)) + ggplot2::facet_wrap(~BY, ncol = 3L) + scale_color_jco() +
  labs(x="Support by Prior Country Perception (5-cat)")+ theme(legend.position="none")

ggsave("Output/Rplot_byprior.pdf",
       width = 8, height = 10, dpi = 300, units = "in", device='pdf')


# By prior view on country 2
model <- DV_support ~ Policy + Background + badbehavior1 * country1 + Inaction + Author + Rationale + EffectOrd

mms <- cj(combo, model, 
          id = ~1, alpha=0.05, estimate = "mm", by = ~ Prior3)

diff_mms <- cj(combo, model,
               id = ~1, alpha=0.05, estimate = "mm_diff", by = ~ Prior3)

plot(rbind(mms, diff_mms)) + ggplot2::facet_wrap(~BY, ncol = 3L) + scale_color_jco() +
  labs(x="Support by Prior Country Perception (3-cat)")+ theme(legend.position="none")

ggsave("Output/Rplot_byprior3.pdf",
       width = 8, height = 10, dpi = 300, units = "in", device='pdf')

# By Policy and party
model <- DV_support ~ Background + badbehavior1 * country1 + Inaction + Author + Rationale + EffectOrd + Prior3


amces <- cj(combo, model, 
            id = ~1, alpha=0.05, estimate = "amce", by = ~ Policy*party3)

plot(rbind(amces)) + ggplot2::facet_wrap(~BY, ncol = 3L) + scale_color_jco() +
  labs(x="Support by Policy and Party")+ theme(legend.position="none")

ggsave("Output/Rplot_bypolparty.pdf",
       width = 8, height = 9, dpi = 300, units = "in", device='pdf')


# By policy and bad behavior
model <- DV_support ~ Background + country1 + Inaction + Author + Rationale + EffectOrd + Prior3

amces <- cj(combo, model, 
            id = ~1, alpha=0.05, estimate = "amce", by = ~ Policy*badbehavior1)

plot(rbind(amces)) + ggplot2::facet_wrap(~BY, ncol = 4L) + scale_color_jco() +
  labs(x="Support by Policy and issue")+ theme(legend.position="none")

ggsave("Output/Rplot_bypolissue.pdf",
       width = 10, height = 10, dpi = 300, units = "in", device='pdf')


# By country and policy
model <- DV_support ~ Background + badbehavior1 + Inaction + Author + Rationale + EffectOrd + Prior3

amces <- cj(combo, model, 
            id = ~1, alpha=0.05, estimate = "amce", by = ~ country1*Policy)

plot(rbind(amces)) + ggplot2::facet_wrap(~BY, ncol = 7L) + scale_color_jco() +
  labs(x="Support by Country and Policy")+ theme(legend.position="none")

ggsave("Output/Rplot_bycountrypolicy.pdf",
       width = 14, height = 16, dpi = 300, units = "in", device='pdf')


### 5-CAT prior 
#H3 and H6 (plus others subset as well)
model_bypol_full <- DV_support ~ Background + Prior + badbehavior1*country1 + Inaction + Author + Rationale + EffectOrd

amces_bypol_full <- cj(combo, model_bypol_full, 
                       id = ~1, alpha=0.05, estimate = "amce", by = ~ Policy)


amces_bypol_full <- amces_bypol_full %>%
  mutate(feature = recode(feature, "badbehavior1" = "Issue", "Inaction" = "If no action", "Prior" = "Preexisting attitude", 
                          "EffectOrd" = "Effect on target economy", "country1" = "Country"),
         level = recode(level, "terror" = "Terror", "drugs" = "Drugs", 
                        "developnukes" = "WMD", "stopdemocracy" = "Democracy crackdown",
                        "TickingClock" = "Ticking clock", "Continue" = "Continuation",
                        "Appointee_only" = "Appointee", "Appointee_donor" = "Appointee donor",
                        "Professional_expert" = "Professional expert"))


plot(rbind(amces_bypol_full)) + ggplot2::facet_wrap(~BY, ncol = 4L) + scale_color_jco() +
  labs(x="Effect on support by Policy") + theme(legend.position="none")

ggsave("Output/Rplot_bypol_full_5.pdf",
       width = 6.5, height = 6, dpi = 300, units = "in", device='pdf')

amces_bypol_base <- amces_bypol_full %>%
  filter(feature != "Country" & feature != "Rationale" & feature != "Effect on target economy")


plot(rbind(amces_bypol_base)) + ggplot2::facet_wrap(~BY, ncol = 4L) + scale_colour_viridis_d( begin = 0, end = .7) +
  labs(x="Effect on support by proposed policy") + theme(legend.position="none", axis.title = element_text(size = 8))

ggsave("Output/Rplot_bypol_base_5.pdf",
       width = 6.5, height = 3.75, dpi = 300, units = "in")



model_add_full_interests <- DV_interests ~ Policy + Background + badbehavior1*country1 + Inaction + Author + Rationale + EffectOrd + Prior3


amces_add_full_interests <- cj(combo, model_add_full_interests,
                               id = ~1, alpha=0.05, estimate = "amce")

amces_add_full_interests <- amces_add_full_interests %>%
  mutate(feature = recode(feature, "badbehavior1" = "Issue", "Inaction" = "If no action", 
                          "EffectOrd" = "Effect on target economy", "country1" = "Country",
                          "Policy" = "Proposed policy", "Prior3" = "Prior attitude"),
         level = recode(level, "terror" = "Terror", "drugs" = "Drugs", 
                        "developnukes" = "WMD", "stopdemocracy" = "Democracy crackdown",
                        "TickingClock" = "Ticking clock", "Continue" = "Continuation",
                        "Appointee_only" = "Appointee", "Appointee_donor" = "Appointee donor",
                        "Professional_expert" = "Professional expert"))

amce_int <- plot(rbind(amces_add_full_interests)) + scale_colour_viridis_d( begin = 0, end = .7) +
  labs(x="Average marginal component effects")+ theme(legend.position="none")


mm_add_full_interests <- cj(combo, model_add_full_interests,
                            id = ~1, alpha=0.05, estimate = "mm")

mm_add_full_interests <- mm_add_full_interests %>%
  mutate(feature = recode(feature, "badbehavior1" = "Issue", "Inaction" = "If no action", 
                          "EffectOrd" = "Effect on target economy", "country1" = "Country",
                          "Policy" = "Proposed policy", "Prior3" = "Prior attitude"),
         level = recode(level, "terror" = "Terror", "drugs" = "Drugs", 
                        "developnukes" = "WMD", "stopdemocracy" = "Democracy crackdown",
                        "TickingClock" = "Ticking clock", "Continue" = "Continuation",
                        "Appointee_only" = "Appointee", "Appointee_donor" = "Appointee donor",
                        "Professional_expert" = "Professional expert"))

mm_int <- plot(rbind(mm_add_full_interests)) + scale_colour_viridis_d( begin = 0, end = .7) +
  labs(x="Marginal means")+ theme(legend.position="none")

ggarrange(amce_int, mm_int, ncol = 2)


ggsave("Output/Rplot_additive_full_int.pdf",
       width = 10, height = 10, dpi = 300, units = "in", device='pdf')



#H3 and H6 (plus others subset as well) - marginal means
model_bypol_full_interests <- DV_interests ~ Background + Prior3 + badbehavior1*country1 + Inaction + Author + Rationale + EffectOrd 

mms_bypol_full_interests <- cj(combo, model_bypol_full_interests, 
                               id = ~1, alpha=0.05, estimate = "mm", by = ~ Policy)

diff_mms_mms_bypol_full_interests <- cj(combo, model_bypol_full_interests,
                                        id = ~1, alpha=0.05, estimate = "mm_diff", by = ~ Policy)


mms_bypol_full_interests <- mms_bypol_full_interests %>%
  mutate(feature = recode(feature, "badbehavior1" = "Issue", "Inaction" = "If no action", "Prior3" = "Prior attitude", 
                          "EffectOrd" = "Effect on target economy", "country1" = "Country"),
         level = recode(level, "terror" = "Terror", "drugs" = "Drugs", 
                        "developnukes" = "WMD", "stopdemocracy" = "Democracy crackdown",
                        "TickingClock" = "Ticking clock", "Continue" = "Continuation",
                        "Appointee_only" = "Appointee", "Appointee_donor" = "Appointee donor",
                        "Professional_expert" = "Professional expert"))

diff_mms_mms_bypol_full_interests <- diff_mms_mms_bypol_full_interests %>%
  mutate(feature = recode(feature, "badbehavior1" = "Issue", "Inaction" = "If no action", "Prior3" = "Prior attitude", 
                          "EffectOrd" = "Effect on target economy", "country1" = "Country"),
         level = recode(level, "terror" = "Terror", "drugs" = "Drugs", 
                        "developnukes" = "WMD", "stopdemocracy" = "Democracy crackdown",
                        "TickingClock" = "Ticking clock", "Continue" = "Continuation",
                        "Appointee_only" = "Appointee", "Appointee_donor" = "Appointee donor",
                        "Professional_expert" = "Professional expert"))

plot(rbind(mms_bypol_full_interests, diff_mms_mms_bypol_full_interests)) + ggplot2::facet_wrap(~BY, ncol = 4L) + scale_color_jco() +
  labs(x="Marginal means") + theme(legend.position="none")

ggsave("Output/Rplot_mm_bypol_full_interests.pdf",
       width = 8, height = 8, dpi = 300, units = "in", device='pdf')




model_add_full_costly <- DV_costly ~ Policy + Background + badbehavior1*country1 + Inaction + Author + Rationale + EffectOrd + Prior3


amces_add_full_costly <- cj(combo, model_add_full_costly,
                            id = ~1, alpha=0.05, estimate = "amce")

amces_add_full_costly <- amces_add_full_costly %>%
  mutate(feature = recode(feature, "badbehavior1" = "Issue", "Inaction" = "If no action", 
                          "EffectOrd" = "Effect on target economy", "country1" = "Country",
                          "Policy" = "Proposed policy", "Prior3" = "Prior attitude"),
         level = recode(level, "terror" = "Terror", "drugs" = "Drugs", 
                        "developnukes" = "WMD", "stopdemocracy" = "Democracy crackdown",
                        "TickingClock" = "Ticking clock", "Continue" = "Continuation",
                        "Appointee_only" = "Appointee", "Appointee_donor" = "Appointee donor",
                        "Professional_expert" = "Professional expert"))

amce_cost <- plot(rbind(amces_add_full_costly)) + scale_colour_viridis_d( begin = 0, end = .7) +
  labs(x="Average marginal component effects")+ theme(legend.position="none")


mm_add_full_costly <- cj(combo, model_add_full_costly,
                         id = ~1, alpha=0.05, estimate = "mm")

mm_add_full_costly <- mm_add_full_costly %>%
  mutate(feature = recode(feature, "badbehavior1" = "Issue", "Inaction" = "If no action", 
                          "EffectOrd" = "Effect on target economy", "country1" = "Country",
                          "Policy" = "Proposed policy", "Prior3" = "Prior attitude"),
         level = recode(level, "terror" = "Terror", "drugs" = "Drugs", 
                        "developnukes" = "WMD", "stopdemocracy" = "Democracy crackdown",
                        "TickingClock" = "Ticking clock", "Continue" = "Continuation",
                        "Appointee_only" = "Appointee", "Appointee_donor" = "Appointee donor",
                        "Professional_expert" = "Professional expert"))

mm_cost <- plot(rbind(mm_add_full_costly)) + scale_colour_viridis_d( begin = 0, end = .7) +
  labs(x="Marginal means")+ theme(legend.position="none")

ggarrange(amce_cost, mm_cost, ncol = 2)


ggsave("Output/Rplot_additive_full_cost.pdf",
       width = 10, height = 10, dpi = 300, units = "in", device='pdf')


#H3 and H6 (plus others subset as well) - marginal means
model_bypol_full_costly <- DV_costly ~ Background + Prior3 + badbehavior1*country1 + Inaction + Author + Rationale + EffectOrd 

mms_bypol_full_costly <- cj(combo, model_bypol_full_costly, 
                            id = ~1, alpha=0.05, estimate = "mm", by = ~ Policy)

diff_mms_mms_bypol_full_costly <- cj(combo, model_bypol_full_costly,
                                     id = ~1, alpha=0.05, estimate = "mm_diff", by = ~ Policy)


mms_bypol_full_costly <- mms_bypol_full_costly %>%
  mutate(feature = recode(feature, "badbehavior1" = "Issue", "Inaction" = "If no action", "Prior3" = "Prior attitude", 
                          "EffectOrd" = "Effect on target economy", "country1" = "Country"),
         level = recode(level, "terror" = "Terror", "drugs" = "Drugs", 
                        "developnukes" = "WMD", "stopdemocracy" = "Democracy crackdown",
                        "TickingClock" = "Ticking clock", "Continue" = "Continuation",
                        "Appointee_only" = "Appointee", "Appointee_donor" = "Appointee donor",
                        "Professional_expert" = "Professional expert"))

diff_mms_mms_bypol_full_costly <- diff_mms_mms_bypol_full_costly %>%
  mutate(feature = recode(feature, "badbehavior1" = "Issue", "Inaction" = "If no action", "Prior3" = "Prior attitude", 
                          "EffectOrd" = "Effect on target economy", "country1" = "Country"),
         level = recode(level, "terror" = "Terror", "drugs" = "Drugs", 
                        "developnukes" = "WMD", "stopdemocracy" = "Democracy crackdown",
                        "TickingClock" = "Ticking clock", "Continue" = "Continuation",
                        "Appointee_only" = "Appointee", "Appointee_donor" = "Appointee donor",
                        "Professional_expert" = "Professional expert"))

plot(rbind(mms_bypol_full_costly, diff_mms_mms_bypol_full_costly)) + ggplot2::facet_wrap(~BY, ncol = 4L) + scale_color_jco() +
  labs(x="Marginal means") + theme(legend.position="none")

ggsave("Output/Rplot_mm_bypol_full_costly.pdf",
       width = 8, height = 8, dpi = 300, units = "in", device='pdf')



model_add_full_effective <- DV_effective ~ Policy + Background + badbehavior1*country1 + Inaction + Author + Rationale + EffectOrd + Prior3


amces_add_full_effective <- cj(combo, model_add_full_effective,
                               id = ~1, alpha=0.05, estimate = "amce")

amces_add_full_effective <- amces_add_full_effective %>%
  mutate(feature = recode(feature, "badbehavior1" = "Issue", "Inaction" = "If no action", 
                          "EffectOrd" = "Effect on target economy", "country1" = "Country",
                          "Policy" = "Proposed policy", "Prior3" = "Prior attitude"),
         level = recode(level, "terror" = "Terror", "drugs" = "Drugs", 
                        "developnukes" = "WMD", "stopdemocracy" = "Democracy crackdown",
                        "TickingClock" = "Ticking clock", "Continue" = "Continuation",
                        "Appointee_only" = "Appointee", "Appointee_donor" = "Appointee donor",
                        "Professional_expert" = "Professional expert"))

amce_eff <- plot(rbind(amces_add_full_effective)) + scale_colour_viridis_d( begin = 0, end = .7) +
  labs(x="Average marginal component effects")+ theme(legend.position="none")


mm_add_full_effective <- cj(combo, model_add_full_effective,
                            id = ~1, alpha=0.05, estimate = "mm")

mm_add_full_effective <- mm_add_full_effective %>%
  mutate(feature = recode(feature, "badbehavior1" = "Issue", "Inaction" = "If no action", 
                          "EffectOrd" = "Effect on target economy", "country1" = "Country",
                          "Policy" = "Proposed policy", "Prior3" = "Prior attitude"),
         level = recode(level, "terror" = "Terror", "drugs" = "Drugs", 
                        "developnukes" = "WMD", "stopdemocracy" = "Democracy crackdown",
                        "TickingClock" = "Ticking clock", "Continue" = "Continuation",
                        "Appointee_only" = "Appointee", "Appointee_donor" = "Appointee donor",
                        "Professional_expert" = "Professional expert"))

mm_eff <- plot(rbind(mm_add_full_effective)) + scale_colour_viridis_d( begin = 0, end = .7) +
  labs(x="Marginal means")+ theme(legend.position="none")

ggarrange(amce_eff, mm_eff, ncol = 2)


ggsave("Output/Rplot_additive_full_eff.pdf",
       width = 10, height = 10, dpi = 300, units = "in", device='pdf')




#H3 and H6 (plus others subset as well) - marginal means
model_bypol_full_effective <- DV_effective ~ Background + Prior3 + badbehavior1*country1 + Inaction + Author + Rationale + EffectOrd 

mms_bypol_full_effective <- cj(combo, model_bypol_full_effective, 
                               id = ~1, alpha=0.05, estimate = "mm", by = ~ Policy)

diff_mms_mms_bypol_full_effective <- cj(combo, model_bypol_full_effective,
                                        id = ~1, alpha=0.05, estimate = "mm_diff", by = ~ Policy)


mms_bypol_full_effective <- mms_bypol_full_effective %>%
  mutate(feature = recode(feature, "badbehavior1" = "Issue", "Inaction" = "If no action", "Prior3" = "Prior attitude", 
                          "EffectOrd" = "Effect on target economy", "country1" = "Country"),
         level = recode(level, "terror" = "Terror", "drugs" = "Drugs", 
                        "developnukes" = "WMD", "stopdemocracy" = "Democracy crackdown",
                        "TickingClock" = "Ticking clock", "Continue" = "Continuation",
                        "Appointee_only" = "Appointee", "Appointee_donor" = "Appointee donor",
                        "Professional_expert" = "Professional expert"))

diff_mms_mms_bypol_full_effective <- diff_mms_mms_bypol_full_effective %>%
  mutate(feature = recode(feature, "badbehavior1" = "Issue", "Inaction" = "If no action", "Prior3" = "Prior attitude", 
                          "EffectOrd" = "Effect on target economy", "country1" = "Country"),
         level = recode(level, "terror" = "Terror", "drugs" = "Drugs", 
                        "developnukes" = "WMD", "stopdemocracy" = "Democracy crackdown",
                        "TickingClock" = "Ticking clock", "Continue" = "Continuation",
                        "Appointee_only" = "Appointee", "Appointee_donor" = "Appointee donor",
                        "Professional_expert" = "Professional expert"))

plot(rbind(mms_bypol_full_effective, diff_mms_mms_bypol_full_effective)) + ggplot2::facet_wrap(~BY, ncol = 4L) + scale_color_jco() +
  labs(x="Marginal means") + theme(legend.position="none")

ggsave("Output/Rplot_mm_bypol_full_effective.pdf",
       width = 8, height = 8, dpi = 300, units = "in", device='pdf')




model_add_full_strength <- DV_strength ~ Policy + Background + badbehavior1*country1 + Inaction + Author + Rationale + EffectOrd + Prior3


amces_add_full_strength <- cj(combo, model_add_full_strength,
                              id = ~1, alpha=0.05, estimate = "amce")

amces_add_full_strength <- amces_add_full_strength %>%
  mutate(feature = recode(feature, "badbehavior1" = "Issue", "Inaction" = "If no action", 
                          "EffectOrd" = "Effect on target economy", "country1" = "Country",
                          "Policy" = "Proposed policy", "Prior3" = "Prior attitude"),
         level = recode(level, "terror" = "Terror", "drugs" = "Drugs", 
                        "developnukes" = "WMD", "stopdemocracy" = "Democracy crackdown",
                        "TickingClock" = "Ticking clock", "Continue" = "Continuation",
                        "Appointee_only" = "Appointee", "Appointee_donor" = "Appointee donor",
                        "Professional_expert" = "Professional expert"))

amce_str <- plot(rbind(amces_add_full_strength)) + scale_colour_viridis_d( begin = 0, end = .7) +
  labs(x="Average marginal component effects")+ theme(legend.position="none")


mm_add_full_strength <- cj(combo, model_add_full_strength,
                           id = ~1, alpha=0.05, estimate = "mm")

mm_add_full_strength <- mm_add_full_strength %>%
  mutate(feature = recode(feature, "badbehavior1" = "Issue", "Inaction" = "If no action", 
                          "EffectOrd" = "Effect on target economy", "country1" = "Country",
                          "Policy" = "Proposed policy", "Prior3" = "Prior attitude"),
         level = recode(level, "terror" = "Terror", "drugs" = "Drugs", 
                        "developnukes" = "WMD", "stopdemocracy" = "Democracy crackdown",
                        "TickingClock" = "Ticking clock", "Continue" = "Continuation",
                        "Appointee_only" = "Appointee", "Appointee_donor" = "Appointee donor",
                        "Professional_expert" = "Professional expert"))

mm_str <- plot(rbind(mm_add_full_strength)) + scale_colour_viridis_d( begin = 0, end = .7) +
  labs(x="Marginal means")+ theme(legend.position="none")

ggarrange(amce_str, mm_str, ncol = 2)


ggsave("Output/Rplot_additive_full_str.pdf",
       width = 10, height = 10, dpi = 300, units = "in", device='pdf')



#H3 and H6 (plus others subset as well) - marginal means
model_bypol_full_strength <- DV_strength ~ Background + Prior3 + badbehavior1*country1 + Inaction + Author + Rationale + EffectOrd 

mms_bypol_full_strength <- cj(combo, model_bypol_full_strength, 
                              id = ~1, alpha=0.05, estimate = "mm", by = ~ Policy)

diff_mms_mms_bypol_full_strength <- cj(combo, model_bypol_full_strength,
                                       id = ~1, alpha=0.05, estimate = "mm_diff", by = ~ Policy)


mms_bypol_full_strength <- mms_bypol_full_strength %>%
  mutate(feature = recode(feature, "badbehavior1" = "Issue", "Inaction" = "If no action", "Prior3" = "Prior attitude", 
                          "EffectOrd" = "Effect on target economy", "country1" = "Country"),
         level = recode(level, "terror" = "Terror", "drugs" = "Drugs", 
                        "developnukes" = "WMD", "stopdemocracy" = "Democracy crackdown",
                        "TickingClock" = "Ticking clock", "Continue" = "Continuation",
                        "Appointee_only" = "Appointee", "Appointee_donor" = "Appointee donor",
                        "Professional_expert" = "Professional expert"))

diff_mms_mms_bypol_full_strength <- diff_mms_mms_bypol_full_strength %>%
  mutate(feature = recode(feature, "badbehavior1" = "Issue", "Inaction" = "If no action", "Prior3" = "Prior attitude", 
                          "EffectOrd" = "Effect on target economy", "country1" = "Country"),
         level = recode(level, "terror" = "Terror", "drugs" = "Drugs", 
                        "developnukes" = "WMD", "stopdemocracy" = "Democracy crackdown",
                        "TickingClock" = "Ticking clock", "Continue" = "Continuation",
                        "Appointee_only" = "Appointee", "Appointee_donor" = "Appointee donor",
                        "Professional_expert" = "Professional expert"))

plot(rbind(mms_bypol_full_strength, diff_mms_mms_bypol_full_strength)) + ggplot2::facet_wrap(~BY, ncol = 4L) + scale_color_jco() +
  labs(x="Marginal means") + theme(legend.position="none")

ggsave("Output/Rplot_mm_bypol_full_strength.pdf",
       width = 8, height = 8, dpi = 300, units = "in", device='pdf')


model_add_full_message <- DV_message ~ Policy + Background + badbehavior1*country1 + Inaction + Author + Rationale + EffectOrd + Prior3


amces_add_full_message <- cj(combo, model_add_full_message,
                             id = ~1, alpha=0.05, estimate = "amce")

amces_add_full_message <- amces_add_full_message %>%
  mutate(feature = recode(feature, "badbehavior1" = "Issue", "Inaction" = "If no action", 
                          "EffectOrd" = "Effect on target economy", "country1" = "Country",
                          "Policy" = "Proposed policy", "Prior3" = "Prior attitude"),
         level = recode(level, "terror" = "Terror", "drugs" = "Drugs", 
                        "developnukes" = "WMD", "stopdemocracy" = "Democracy crackdown",
                        "TickingClock" = "Ticking clock", "Continue" = "Continuation",
                        "Appointee_only" = "Appointee", "Appointee_donor" = "Appointee donor",
                        "Professional_expert" = "Professional expert"))

amce_mes <- plot(rbind(amces_add_full_message)) + scale_colour_viridis_d( begin = 0, end = .7) +
  labs(x="Average marginal component effects")+ theme(legend.position="none")


mm_add_full_message <- cj(combo, model_add_full_message,
                          id = ~1, alpha=0.05, estimate = "mm")

mm_add_full_message <- mm_add_full_message %>%
  mutate(feature = recode(feature, "badbehavior1" = "Issue", "Inaction" = "If no action", 
                          "EffectOrd" = "Effect on target economy", "country1" = "Country",
                          "Policy" = "Proposed policy", "Prior3" = "Prior attitude"),
         level = recode(level, "terror" = "Terror", "drugs" = "Drugs", 
                        "developnukes" = "WMD", "stopdemocracy" = "Democracy crackdown",
                        "TickingClock" = "Ticking clock", "Continue" = "Continuation",
                        "Appointee_only" = "Appointee", "Appointee_donor" = "Appointee donor",
                        "Professional_expert" = "Professional expert"))

mm_mes <- plot(rbind(mm_add_full_message)) + scale_colour_viridis_d( begin = 0, end = .7) +
  labs(x="Marginal means")+ theme(legend.position="none")

ggarrange(amce_mes, mm_mes, ncol = 2)


ggsave("Output/Rplot_additive_full_mes.pdf",
       width = 10, height = 10, dpi = 300, units = "in", device='pdf')


#H3 and H6 (plus others subset as well) - marginal means
model_bypol_full_message <- DV_message ~ Background + Prior3 + badbehavior1*country1 + Inaction + Author + Rationale + EffectOrd 

mms_bypol_full_message <- cj(combo, model_bypol_full_message, 
                             id = ~1, alpha=0.05, estimate = "mm", by = ~ Policy)

diff_mms_mms_bypol_full_message <- cj(combo, model_bypol_full_message,
                                      id = ~1, alpha=0.05, estimate = "mm_diff", by = ~ Policy)


mms_bypol_full_message <- mms_bypol_full_message %>%
  mutate(feature = recode(feature, "badbehavior1" = "Issue", "Inaction" = "If no action", "Prior3" = "Prior attitude", 
                          "EffectOrd" = "Effect on target economy", "country1" = "Country"),
         level = recode(level, "terror" = "Terror", "drugs" = "Drugs", 
                        "developnukes" = "WMD", "stopdemocracy" = "Democracy crackdown",
                        "TickingClock" = "Ticking clock", "Continue" = "Continuation",
                        "Appointee_only" = "Appointee", "Appointee_donor" = "Appointee donor",
                        "Professional_expert" = "Professional expert"))

diff_mms_mms_bypol_full_message <- diff_mms_mms_bypol_full_message %>%
  mutate(feature = recode(feature, "badbehavior1" = "Issue", "Inaction" = "If no action", "Prior3" = "Prior attitude", 
                          "EffectOrd" = "Effect on target economy", "country1" = "Country"),
         level = recode(level, "terror" = "Terror", "drugs" = "Drugs", 
                        "developnukes" = "WMD", "stopdemocracy" = "Democracy crackdown",
                        "TickingClock" = "Ticking clock", "Continue" = "Continuation",
                        "Appointee_only" = "Appointee", "Appointee_donor" = "Appointee donor",
                        "Professional_expert" = "Professional expert"))

plot(rbind(mms_bypol_full_message, diff_mms_mms_bypol_full_message)) + ggplot2::facet_wrap(~BY, ncol = 4L) + scale_color_jco() +
  labs(x="Marginal means") + theme(legend.position="none")

ggsave("Output/Rplot_mm_bypol_full_message.pdf",
       width = 8, height = 8, dpi = 300, units = "in", device='pdf')



combo$gender <- as.factor(combo$gender)
#H3 and H6 (plus others subset as well)
model_bypol_full_gender <- DV_support ~ Policy + Background + Prior3 + badbehavior1 * country1 + Inaction + Author + Rationale + EffectOrd

amces_bypol_full_gender <- cj(combo, model_bypol_full_gender, 
                              id = ~1, alpha=0.05, estimate = "amce", by = ~ gender)


amces_bypol_full_gender <- amces_bypol_full_gender %>%
  mutate(feature = recode(feature, "badbehavior1" = "Issue", "Inaction" = "If no action", "Prior3" = "Prior attitude", 
                          "EffectOrd" = "Effect on target economy", "country1" = "Country"),
         level = recode(level, "terror" = "Terror", "drugs" = "Drugs", 
                        "developnukes" = "WMD", "stopdemocracy" = "Democracy crackdown",
                        "TickingClock" = "Ticking clock", "Continue" = "Continuation",
                        "Appointee_only" = "Appointee", "Appointee_donor" = "Appointee donor",
                        "Professional_expert" = "Professional expert"))


plot(rbind(amces_bypol_full_gender)) + ggplot2::facet_wrap(~BY, ncol = 4L) + scale_color_jco() +
  labs(x="Average marginal component effects") + theme(legend.position="none")

ggsave("Output/Rplot_bypol_full_gender.pdf",
       width = 10, height = 8, dpi = 300, units = "in", device='pdf')




#H3 and H6 (plus others subset as well) - marginal means
model_bypol_full_gender <- DV_support ~ Policy + Background + Prior3 + badbehavior1*country1 + Inaction + Author + Rationale + EffectOrd 

mms_bypol_full_gender <- cj(combo, model_bypol_full_gender, 
                            id = ~1, alpha=0.05, estimate = "mm", by = ~ gender)

diff_mms_mms_bypol_full_gender <- cj(combo, model_bypol_full_gender,
                                     id = ~1, alpha=0.05, estimate = "mm_diff", by = ~ gender)


mms_bypol_full_gender <- mms_bypol_full_gender %>%
  mutate(feature = recode(feature, "badbehavior1" = "Issue", "Inaction" = "If no action", "Prior3" = "Prior attitude", 
                          "EffectOrd" = "Effect on target economy", "country1" = "Country"),
         level = recode(level, "terror" = "Terror", "drugs" = "Drugs", 
                        "developnukes" = "WMD", "stopdemocracy" = "Democracy crackdown",
                        "TickingClock" = "Ticking clock", "Continue" = "Continuation",
                        "Appointee_only" = "Appointee", "Appointee_donor" = "Appointee donor",
                        "Professional_expert" = "Professional expert"))

diff_mms_mms_bypol_full_gender <- diff_mms_mms_bypol_full_gender %>%
  mutate(feature = recode(feature, "badbehavior1" = "Issue", "Inaction" = "If no action", "Prior3" = "Prior attitude", 
                          "EffectOrd" = "Effect on target economy", "country1" = "Country"),
         level = recode(level, "terror" = "Terror", "drugs" = "Drugs", 
                        "developnukes" = "WMD", "stopdemocracy" = "Democracy crackdown",
                        "TickingClock" = "Ticking clock", "Continue" = "Continuation",
                        "Appointee_only" = "Appointee", "Appointee_donor" = "Appointee donor",
                        "Professional_expert" = "Professional expert"))

plot(rbind(mms_bypol_full_gender, diff_mms_mms_bypol_full_gender)) + ggplot2::facet_wrap(~BY, ncol = 4L) + scale_color_jco() +
  labs(x="Marginal means") + theme(legend.position="none")

ggsave("Output/Rplot_mm_bypol_full_gender.pdf",
       width = 10, height = 10, dpi = 300, units = "in", device='pdf')


## Bar chart of countries
ggplot(data = combo, aes(country1)) +
  geom_bar(aes()) +
  xlab("Country") + ylab("Count") +
  theme(text = element_text(size=9),
        axis.text.x = element_text(angle=90, hjust=1)) 

ggsave("Output/Rplot_randomization_country.pdf",
       width = 4.5, height = 4, dpi = 300, units = "in")



#########################
### Check 5-cat prior ###
#########################

combo <- combo %>%
  mutate(Prior=fct_relevel(Prior,c("Very unfavorable","Somewhat unfavorable",
                                   "Neutral", "Somewhat favorable", "Very favorable")))

ggplot(data = combo[!is.na(combo$Prior),], aes(Prior)) +
  facet_wrap("country1") +
  geom_bar(aes()) +   xlab("Preexisting favorability") + ylab("Count") +
  theme(text = element_text(size=9),
        axis.text.x = element_text(angle=90, hjust=1)) 

ggsave("Output/Rplot_priorbycountry5.pdf",
       width = 8, height = 8, dpi = 300, units = "in")

# return reference level
combo <- combo %>%
  mutate(Prior=fct_relevel(Prior,c("Neutral", "Very unfavorable","Somewhat unfavorable",
                                   "Somewhat favorable", "Very favorable")))

combo <- combo %>%
  mutate(Prior3 = fct_relevel(Prior3, c("Unfavorable", "Neutral", "Favorable")))

 ggplot(data = combo[!is.na(combo$Prior),], aes(Prior3)) +
  facet_wrap("country1") +
  geom_bar(aes()) +   xlab("Preexisting favorability") + ylab("Count") +
  theme(text = element_text(size=9),
        axis.text.x = element_text(angle=90, hjust=1)) 

 ggsave("Output/Rplot_priorbycountry.pdf",
       width = 8, height = 8, dpi = 300, units = "in")

# return reference level
combo <- combo %>%
  mutate(Prior3=fct_relevel(Prior3,c("Neutral", "Unfavorable", "Favorable")))



#####################
### Summary stats ###
#####################

combo <- combo %>%
  mutate(agecat = ifelse(as.numeric(age) < 30, "Under 30", 
                         ifelse(as.numeric(age) >= 30 & as.numeric(age) < 50, "30-49",
                                ifelse(as.numeric(age) >= 50 & as.numeric(age) < 66, "50-65",
                                       ifelse(as.numeric(age) >= 66, "66 or older", NA)))),
         edcat = ifelse(as.numeric(education) < 0, "None of above", 
                        ifelse(as.numeric(education) == 1, "Some HS or less",
                               ifelse(as.numeric(education) == 2, "HS grad",
                                      ifelse(as.numeric(education) == 3 | 
                                               as.numeric(education) == 4 | 
                                               as.numeric(education) == 5, "Post-HS up to Associates",
                                             ifelse(as.numeric(education) == 6, "Bachelors degree",
                                                    ifelse(as.numeric(education) > 6, "Graduate degree", NA)))))),
         gender = ifelse(gender == "1", "Male",
                         ifelse(gender == "2", "Female", NA)),
         
         Black = ifelse(ethnicity == 2, "Black resp.", "Non-Black resp."),
         Hispanic = ifelse(hispanic != 1 & hispanic != 15, "Hispanic resp.", "Non-Hispanic resp.")
  )

combo$gender <- as.factor(combo$gender)
combo$Black <- as.factor(combo$Black)
combo$Hispanic <- as.factor(combo$Hispanic)


combo$agecat <- as.factor(combo$agecat)
combo$agecat <- relevel(combo$agecat, "Under 30", "30-49", "50-65", "66 or older")

combo$edcat <- as.factor(combo$edcat)

combo$edcat <- relevel(combo$edcat, "Some HS or less", "HS grad", "Post-HS up to Associates", "Bachelors degree", "Graduate degree", "None of above")





stats2p <- combo %>%
  filter(!is.na(combo$DV_support) & !is.na(combo$Prior3) & !is.na(combo$EffectOrd)) %>%
  select(Policy, party3, gender, Black, Hispanic, edcat, agecat, urban_rural)

stats2b <- combo %>%
  filter(!is.na(combo$DV_support) & !is.na(combo$Prior3) & !is.na(combo$EffectOrd)) %>%
  select(Background, party3, gender, Black, Hispanic, edcat, agecat, urban_rural)

stats2bb <- combo %>%
  filter(!is.na(combo$DV_support) & !is.na(combo$Prior3) & !is.na(combo$EffectOrd)) %>%
  select(badbehavior1, party3, gender, Black, Hispanic, edcat, agecat, urban_rural)

stats2i <- combo %>%
  filter(!is.na(combo$DV_support) & !is.na(combo$Prior3) & !is.na(combo$EffectOrd)) %>%
  select(Inaction, party3, gender, Black, Hispanic, edcat, agecat, urban_rural)

stats2a <- combo %>%
  filter(!is.na(combo$DV_support) & !is.na(combo$Prior3) & !is.na(combo$EffectOrd)) %>%
  select(Author, party3, gender, Black, Hispanic, edcat, agecat, urban_rural)

statsall <- combo %>%
  filter(!is.na(combo$DV_support) & !is.na(combo$Prior3) & !is.na(combo$EffectOrd)) %>%
  select(party3, gender, Black, Hispanic, edcat, agecat, urban_rural)


demo_by_pol <- tbl_summary(stats2p, by = Policy) %>% add_p() %>% bold_labels() %>% as_gt() 

gt::gtsave(demo_by_pol, filename = "Output/summary_stat2p.tex") 

demo_by_back <- tbl_summary(stats2b, by = Background) %>% add_p() %>% bold_labels() %>% as_gt()

gt::gtsave(demo_by_back, filename = "Output/summary_stat2b.tex")

demo_by_badb <- tbl_summary(stats2bb, by = badbehavior1) %>% add_p() %>% bold_labels() %>% as_gt()

gt::gtsave(demo_by_badb, filename = "Output/summary_stat2bb.tex")

demo_by_inac <- tbl_summary(stats2i, by = Inaction) %>% add_p() %>% bold_labels() %>% as_gt()

gt::gtsave(demo_by_inac, filename = "Output/summary_stat2i.tex")

demo_by_auth <- tbl_summary(stats2a, by = Author) %>% add_p() %>% bold_labels() %>% as_gt()

gt::gtsave(demo_by_auth, filename = "Output/summary_stat2a.tex")

demo_all <- tbl_summary(statsall) %>% bold_labels() %>% as_gt()

gt::gtsave(demo_all, filename = "Output/summary_all.tex")


